import random


class AI:
    def __init__(self, difficulty="easy_ai"):
        self.difficulty = difficulty

    def choose_action(self, enemy_pokemon, player_pokemon):
        if self.difficulty == "easy":
            return self.easy_ai(enemy_pokemon)
        elif self.difficulty == "medium":
            return self.medium_ai(enemy_pokemon, player_pokemon)
        else:
            return self.hard_ai(enemy_pokemon, player_pokemon)
    
    def easy_ai(self, enemy_pokemon):
        available_skills = [i for i, skill in enumerate(enemy_pokemon.skills_deck) if skill.current_pp > 0]
        if available_skills:
            return random.choice(available_skills)
        return 0
        
    def medium_ai(self, enemy_pokemon, player_pokemon):
        from data.attribute_restraint import Attribute
        number = random.random()
        if number >= 0.5:
            best_skill_index = 0
            best_score = 0
            
            for i, skill in enumerate(enemy_pokemon.skills_deck):
                if skill.current_pp <= 0:
                    continue
    
                score = skill.power
                multiplier = Attribute.get_restraint_multiplier(skill.type, player_pokemon.attribute)
                
                if multiplier == 2:
                    score *= 1.5
                elif multiplier == 0.5:
                    score *= 0.7
                
                if score > best_score:
                    best_score = score
                    best_skill_index = i
            
            return best_skill_index
        else:
            available_skills = [i for i, skill in enumerate(enemy_pokemon.skills_deck) if skill.current_pp > 0]
            if available_skills:
                return random.choice(available_skills)
            return 0

    def hard_ai(self, enemy_pokemon, player_pokemon):
        from data.attribute_restraint import Attribute
        best_skill_index = 0
        best_score = 0
        
        for i, skill in enumerate(enemy_pokemon.skills_deck):
            if skill.current_pp <= 0:
                continue

            score = skill.power
            multiplier = Attribute.get_restraint_multiplier(skill.type, player_pokemon.attribute)
                
            
            if multiplier >= 2:
                score *= 1.5
            elif multiplier <= 0.5:
                score *= 0.7
            
            if score > best_score:
                best_score = score
                best_skill_index = i
        
        return best_skill_index



            




        

        